package com.lmaos.basetools.base;

import java.math.BigDecimal;
import java.util.Arrays;
/** 数据 key
 * 
 */
public class SDataKey implements CharSequence{
	
	private final Object[] keys;
	private final String value;
	
	public SDataKey(Object[] keys) {
		this.keys = keys;
		this.value = Arrays.toString(keys);
	}
	public int length() {
 
		return value.length();
	}

	public char charAt(int index) {
		
		return value.charAt(index);
	}
	public CharSequence subSequence(int start, int end) {
		
		return value.subSequence(start, end);
	}
	@Override
	public boolean equals(Object obj) {

		return obj.equals(value);
	}
	@Override
	public int hashCode() {

		return value.hashCode();
	}
	@Override
	public String toString() {
		return value;
	}
	public String toString(String startText,String split,String endText,int startIndex,int endIndex){
		return toString(startText, split, endText, getKeys(startIndex, endIndex));
	}
	public String toString(String startText,String split,String endText){
		return toString(startText, split, endText, keys);
	}
	public String toString(String split,int startIndex,int endIndex){
		return toString(split, getKeys(startIndex, endIndex));
	}
	public String toString(String split){
		return toString(split, keys);
	}
	public String toString(String split,Object[] keys){
		return toString("", split, "", keys);
	}
	public String toString(String startText,String split,String endText,Object[] keys) {
		if(split == null){
			split = "";
		}
		if(startText == null){
			startText = "";
		}
		if(endText == null){
			endText = "";
		}
		StringBuilder text = new StringBuilder(30);
		text.append(startText);
		for (Object key : keys) {
			text.append(key).append(split);
		}
		if(split.length()>0){
			text.replace(text.length()-split.length(), text.length(), endText);
		}
		
		return text.toString();
	}
	public <T>T getObject(int index){
		Object key = keys[index];
		if(key == null){
			return null;
		}
		return (T) key;
	}
	public int getInt(int index){
		Number key = getNumber(index);
		return key.intValue();
	}
	public long getLong(int index){
		Number key = getNumber(index);
		return key.longValue();
	}
	public float getFloat(int index){
		Number key = getNumber(index);
		return key.floatValue();
	}
	public double getDouble(int index){
		Number key = getNumber(index);
		return key.doubleValue();
	}
	public short getShort(int index){
		Number key = getNumber(index);
		return key.shortValue();
	}
	public byte getByte(int index){
		Number key = getNumber(index);
		return key.byteValue();
	}
	public String getString(int index){
		return getObject(index);
	}
	public Number getNumber(int index){
		Object key = keys[index];
		if(key==null){
			return 0;
		}
		if(key instanceof Number){
			return (Number) key;
		}
		try{
			return new BigDecimal(String.valueOf(key));
		}catch(Exception e){
			return 0;
		}
	}
	public Object[] getKeys() {
		return keys;
	}
	public Object[] getKeys(int start,int end) {
		int len = end - start + 1;
		Object[] nkeys = new Object[len];
		System.arraycopy(keys, start, nkeys, 0, len);
		return nkeys;
	}
	public static void main(String[] args) {
		SDataKey x = new SDataKey(new Object[]{1,2,3,4});
		System.out.println(Arrays.toString(x.getKeys()));
		System.out.println(Arrays.toString(x.getKeys(0,1)));
		System.out.println(x.toString("", ",", ""));
		System.out.println(x.toString("", ",", "",1,1));
	}
}
